/*
 * @Author: 晟松
 * @Date: 2021-12-08 20:35:01
 * @LastEditors: 晟松
 * @LastEditTime: 2022-02-04 20:49:49
 * @FilePath: \mini-vue3d:\Code\node\koa2\volunteer-service\src\main\controller\SchoolController.js
 * @Description: 主要是校级管理员和院级管理员的一些操作
 */
import Router from "koa-router";
import koaBodyparser from "koa-bodyparser";
import koaBody from "koa-body";
import SchoolService from "@main/service/SchoolService";
import allowUserIdentity, { isSchoolAdmin } from "@main/middleware/routeMid/validateIdentity";
import { LoginError } from "@main/common/CommonError";

let schoolRouter = new Router({
    prefix: "/api/school",
});
/**
 * @description: 校级审核单个院级
 * @param {*}
 * @return {*}
 */
// 使用多个中间件
schoolRouter.post("/schoolReview", koaBodyparser(), isSchoolAdmin, async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params, ctx.state);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await SchoolService.schoolReview(params));
});
/**
 * @description: 院级辅导员审核单个学生
 * @param {*}
 * @return {*}
 */
schoolRouter.post("/collegeReview", koaBodyparser(), allowUserIdentity("school"), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await SchoolService.collegeReview(params));
});
/**
 * @description: 获得自己学校的所有辅导员信息
 * @param {*}
 * @return {*}
 */
schoolRouter.post("/getAllCollegeAdmin", koaBodyparser(), allowUserIdentity("school"), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await SchoolService.getAllCollegeAdmin(params));
});

/**
 * @description: 查询满足条件的所有自己的学生，查询者是辅导员
 * @param {*}
 * @return {*}
 */
schoolRouter.post("/searchAllCollegeStudent", koaBodyparser(), allowUserIdentity("school"), async (ctx) => {
    let params = ctx.request.body;
    console.log("searchAllCollegeStudent params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await SchoolService.searchAllCollegeStudent(params));
});
/**
 * @description: 获得自己学院的学生信息
 * @param {*}
 * @return {*}
 */
schoolRouter.post("/getAllCollegeStudent", koaBodyparser(), allowUserIdentity("school"), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await SchoolService.getAllCollegeStudent(params));
});

/**
 * @description: 获得所有学生信息，后面可能要实现分页查询的功能
 * @param {*}
 * @return {*}
 */
schoolRouter.post("/getAllStudent", koaBodyparser(), allowUserIdentity("school"), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);
    ctx.body = ctx.renderJson(await SchoolService.getAllStudent(params));
});
/**
 * @description: 通过一些信息查询所有学生
 * @param {*}
 * @return {*}
 */
 schoolRouter.post("/searchAllStudent", koaBodyparser(), allowUserIdentity("school"), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);
    ctx.body = ctx.renderJson(await SchoolService.searchAllStudent(params));
});
/**
 * @description: 上传Excel文件批量注册学生
 * @param {*}
 * @return {*}
 */
schoolRouter.post(
    "/batchRegisterStudent",
    koaBody({
        multipart: true, //支持fordata表单上传文件
        formidable: {
            // 这里还有一些其他的配置，需要时去官网文档看
            maxFileSize: 2000 * 1024 * 1024, // 设置上传文件大小最大限制，默认2M
        },
    }),
    async (ctx) => {
        // files.file里面的 .file是在formdata表单中指定文件的属性名，可自定义
        let files = ctx.request.files.file;
        // console.log("batchRegisterStudent files: ", files);
        let params = ctx.request.body;
        // console.log("params: ", params);
        ctx.body = ctx.renderJson(await SchoolService.batchRegisterStudent(files, params));
    }
);
export default schoolRouter;
